package org.example.mp4demo.mp4;

import lombok.extern.slf4j.Slf4j;
import org.mp4parser.Container;
import org.mp4parser.muxer.Mp4TrackImpl;
import org.mp4parser.muxer.RandomAccessSource;

import java.io.IOException;
@Slf4j
public class CustomMp4TrackImp extends Mp4TrackImpl {

    private RandomAccessSource randomAccess;

    /**
     * Creates a track from a TrackBox and potentially fragments. Use <b>fragements parameter
     * only</b> to supply additional fragments that are not located in the main file.
     *
     * @param trackId      ID of the track to extract
     * @param isofile      the parsed MP4 file
     * @param randomAccess the RandomAccessSource to read the samples from
     * @param name         an arbitrary naem to identify track later - e.g. filename
     */
    public CustomMp4TrackImp(long trackId, Container isofile, RandomAccessSource randomAccess, String name) {
        super(trackId, isofile, randomAccess, name);
        this.randomAccess = randomAccess;
    }

    @Override
    public void close() throws IOException {
        log.info("关闭randomAccess流");
        super.close();
        RandomAccessSource randomAccess1 = this.randomAccess;
        if (randomAccess1 != null) {
            randomAccess1.close();
        }
    }

    public RandomAccessSource getRandomAccess() {
        return randomAccess;
    }

    public void setRandomAccess(RandomAccessSource randomAccess) {
        this.randomAccess = randomAccess;
    }
}
